{extend name="public/base" /}
{block name="style"}
<link rel="stylesheet" href="__THEME__/default/view/admin/static/css/module.css?v={:time_version()}">
{/block}

{block name="content-header"}
<!-- Content Header (Page header) -->
<div class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1 class="m-0 text-dark">
					<h1>数据备份</h1>
				</h1>
			</div><!-- /.col -->
			<div class="col-sm-6">
				<ol class="breadcrumb float-sm-right">
					<li class="breadcrumb-item"><a href="{:Url('index/index')}">首页</a></li>
					<li class="breadcrumb-item"><a href="{:Url('datamanage/index',['type'=>'export'])}">数据备份</a></li>
					<li class="breadcrumb-item active">数据备份</li>
				</ol>
			</div><!-- /.col -->
		</div><!-- /.row -->
	</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
{/block}

{block name="content-content"}
<!-- Main content -->
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">

				<div class="card">
					<div class="card-header border-transparent">
						<div class="pull-left">
							<a href="javascript:location.reload();"><button type="button" class="btn btn-sm btn-info"><i class="fa fa-refresh"></i></button></a>
							<a href="javascript:history.go(-1);"><button type="button" class="btn btn-sm btn-info"><i class="fa fa-reply"></i></button></a>
							<a id="export" href="javascript:;" class="btn btn-sm btn-success" autocomplete="off">立即备份</a>
							<a id="optimize" href="{:Url('optimize')}" class="btn btn-sm btn-success">优化表</a>
							<a id="repair" href="{:Url('repair')}" class="btn btn-sm btn-success">修复表</a>
						</div>
					</div>
					<div class="card-body p-0">
						<div class="table-responsive">
							<form id="export-form" method="post" action="{:Url('export')}">
								<table class="table m-0">
									<thead>
										<tr>
											<th width="48"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
											<th>表名</th>
											<th width="120">数据量</th>
											<th width="120">数据大小</th>
											<th width="160">创建时间</th>
											<th width="160">备份状态</th>
											<th width="120">操作</th>
										</tr>
									</thead>
									<tbody>
										{notempty name="list"}
										{volist name="list" id="table"}
										<tr>
											<td class="num">
												<input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
											</td>
											<td>{$table.name}</td>
											<td>{$table.rows}</td>
											<td>{$table.data_length|format_bytes}</td>
											<td>{$table.create_time}</td>
											<td class="msg">未备份</td>
											<td class="action">
												<a class="ajax-get no-refresh" href="{:Url('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
												<a class="ajax-get no-refresh" href="{:Url('repair?tables='.$table['name'])}">修复表</a>
											</td>
										</tr>
										{/volist}
										{else/}
										<td colspan="9" class="text-center"> aOh! 暂时还没有内容! </td>
										{/notempty}
									</tbody>
								</table>
							</form>
						</div>
						<!-- /.table-responsive -->
					</div>
				</div>

			</div>
		</div>
	</div><!-- /.container-fluid -->
</section>
<!-- /.content -->

<!-- Main content -->
<!-- /.content -->
{/block}

{block name='script'}
<script src="__THEME__/default/view/admin/static/js/common.js?v={:time_version()}"></script>
<script type="text/javascript" charset="utf-8">
highlight_subnav('{:Url('index',['type'=>'export'])}');
(function($){
	var $form = $("#export-form"), $export = $("#export"), tables
		$optimize = $("#optimize"), $repair = $("#repair");

	$optimize.add($repair).click(function(){
		$.post(this.href, $form.serialize(), function(data){
			if(data.code){
				updateAlert(data.msg,'alert-success');
			} else {
				updateAlert(data.msg,'alert-error');
			}
			setTimeout(function(){
				$('#top-alert').find('button').click();
				//$(that).removeClass('disabled').prop('disabled',false);
			},1500);
		}, "json");
		return false;
	});

	$export.click(function(){
		$export.parent().children().addClass("disabled");
		$export.html("正在发送备份请求...");
		$.post(
				$form.attr("action"),
				$form.serialize(),
				function(data){
					if(data.code){
						tables = data.data.tables;
						$export.html(data.msg+ "开始备份，请不要关闭本页面！");
						backup(data.data.tab);
						window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
					} else {
						updateAlert(data.msg,'alert-error');
						$export.parent().children().removeClass("disabled");
						$export.html("立即备份");
						setTimeout(function(){
							$('#top-alert').find('button').click();
							//$(that).removeClass('disabled').prop('disabled',false);
						},1500);
					}
				},
				"json"
			  );
		return false;
	});

	function backup(tab, code){
		code && showmsg(tab.id, "开始备份...(0%)");
		$.get($form.attr("action"), tab, function(data){
			if(data.code){
				showmsg(tab.id, data.msg);

				if(!$.isPlainObject(data.data.tab)){
					$export.parent().children().removeClass("disabled");
					$export.html("备份完成，点击重新备份");
					window.onbeforeunload = function(){ return null }
					return;
				}
				backup(data.data.tab, tab.id != data.data.tab.id);
			} else {
				updateAlert(data.msg,'alert-error');
				$export.parent().children().removeClass("disabled");
				$export.html("立即备份");
				setTimeout(function(){
					$('#top-alert').find('button').click();
					//$(that).removeClass('disabled').prop('disabled',false);
				},1500);
			}
		}, "json");

	}

	function showmsg(id, msg){
		$form.find("input[value=" + tables[id] + "]").closest("tr").find(".msg").html(msg);
	}
})(jQuery);
</script>
{/block}
